using System.Collections.Generic;
using System.Windows.Forms;
using EnvDTE;

namespace Skim.Ssms.AddIn.ActiveFileExplorer.Loader
{
    public abstract class DocumentLoader : IDocumentLoader
    {
        protected static int GetImageIndex(TreeViewImageIndex treeViewImageIndex)
        {
            return (int)treeViewImageIndex;
        }

        public void Load(TreeView treeView, IEnumerable<Document> documents)
        {
            try
            {
                treeView.BeginUpdate();
                treeView.Nodes.Clear();

                LoadTreeView(treeView, documents);

                treeView.ExpandAll();
            }
            finally
            {
                treeView.EndUpdate();
            }
        }

        protected abstract void LoadTreeView(TreeView treeView, IEnumerable<Document> documents);

        /// <summary>
        /// Get a group node within the context
        /// </summary>
        /// <remarks>
        /// If group already exists, then existing group node matching the context is returned else a new one.
        /// </remarks>
        protected virtual TreeNode GetUniqueGroupNode(TreeView treeView, string groupName)
        {
            return treeView.Nodes.ContainsKey(groupName)
                ? treeView.Nodes[groupName]
                : treeView.Nodes.Add(/* key */ groupName, /* text */ groupName);
        }
    }
}